package com.bestcem.xm.user.util;

import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.user.service.MailService;
import com.bestcem.xm.user.service.OrganizationService;
import com.bestcem.xm.user.service.UserService;
import com.bestcem.xm.user.service.dto.user.OrganizationDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户异步方法工具类
 *
 * @author XiaoYunSong <ys.xiao@idiaoyan.com>
 * @version v1.0
 * @date 2022/7/21 15:50
 */
@Slf4j
@Component
public class UserAsyncUtil {

    @Autowired
    private UserService userService;
    @Autowired
    private OrganizationService organizationService;
    @Resource
    private MailService mailService;

    @Async("user_email_executor")
    public void activeEmailManyAsync(List<String> userIds, String remoteHost, String protocol) {
        ServiceResult<String> serviceResult = userService.activeEmailMany(userIds, remoteHost, protocol);
        if (!serviceResult.isSuccess()) {
            log.error("[User] Async activeEmailMany {}-{}-{}  Error; Error msg {}",
                    userIds, remoteHost, protocol, serviceResult.getMsg());
        }
    }

    @Async("user_email_executor")
    public void emailSendAndNotifyOm(@NotNull String[] to,
                                     String[] cc,
                                     String subject,
                                     @NotNull String fileName,
                                     HashMap<String, Object> map) {
        boolean emailSendAndNotifyOm = mailService.emailSendAndNotifyOm(to, cc, subject, fileName, map);
        if (Boolean.FALSE.equals(emailSendAndNotifyOm)) {
            log.error("[User] Async emailSendAndNotifyOm {}-{}-{}  Error; Error msg {}",
                    to, cc, subject, "emailSendAndNotifyOm失败");
        }
    }

    @Async("executor")
    public void notifyCompanyAcceptByEmail(String orgId, String password, String managerEmail, String managerPhone, String username) {
        // 发短信邮件通知
        ServiceResult<OrganizationDTO> orgResult = organizationService.selectByPrimaryKey(orgId);
        OrganizationDTO org = orgResult.getData();
        if (orgResult.isSuccess()) {
            String expireStr = DateUtil.convertDteToString(org.getPackageExpireTime(), DateUtil.YYYY_MM_DD);
            Map<String, String> dataMap = new HashMap<>();
            dataMap.put("code", org.getCode());
            dataMap.put("userName", org.getAdminName());
            dataMap.put("companyName", org.getName());
            dataMap.put("expiryDT", expireStr);
            dataMap.put("passwd", password);
            dataMap.put("manager_email", managerEmail);
            dataMap.put("manager_mobile", managerPhone);
            dataMap.put("org_id", org.getOrgId());
            dataMap.put("username", username);
            try {
                if (StringUtils.isNotEmpty(org.getAdminEmail())) {
                    organizationService.notifyCompanyAcceptByEmail(org.getAdminEmail(), dataMap);
                }
                if (StringUtils.isNotEmpty(org.getAdminPhone())) {
                    organizationService.notifyCompanyAcceptBySms(org.getAdminPhone(), dataMap);
                }
            } catch (Exception e) {
                log.error("[USER] 发送邮件失败", e);
            }
        }
    }
}
